﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class adminstrator_editPatient : System.Web.UI.Page
{
    public OleDbConnection myConnection;
    public static readonly string sconnectionstring = System.Configuration.ConfigurationManager.AppSettings["SQLConnString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        myConnection = new OleDbConnection(sconnectionstring);
        myConnection.Open();
        if (Session["UserName"] == null && Session["UserNo"] == null)
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
                "alert('登录超时（或未登录），请您重新登录！'); parent.location.href='../login.aspx'</script>", false);
        
        if (!IsPostBack)
        {
            string detID = Request.QueryString["deleteID"];
            if (detID != null)
            {
                GridView1.PageIndex = int.Parse(Request.QueryString["pIndex"]);
                delete(detID);
            }
        }    
    }
    protected void ButtonOK_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "")
        {
            Session["sDL"] = "";
            Session["sTB"] = TextBox1.Text.Trim();
            if (DropDownList1.SelectedIndex == 0)
                Session["sDL"] = "ID_JZ";
            else if (DropDownList1.SelectedIndex == 1)
                Session["sDL"] = "Name";
            else if (DropDownList1.SelectedIndex == 2)
                Session["sDL"] = "Snum";
            else if (DropDownList1.SelectedIndex == 3)
                Session["sDL"] = "Phone";
            bind(Session["sDL"].ToString(), Session["sTB"].ToString());
        }       
    }
    protected void ButtonReset_Click(object sender, EventArgs e)
    {

        DropDownList1.SelectedIndex = 0;
        TextBox1.Text = "";
        GridView1.DataSource = null;
        Label2.Text = "";
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string stuID = GridView1.DataKeys[e.NewEditIndex][0].ToString();
        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>location.href='editPatientStart.aspx?stuID=" + stuID + " ';</script>", false);
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
              "if(confirm('确定删除？')){location.href='editPatient.aspx?deleteID=" + GridView1.DataKeys[e.RowIndex][1].ToString() +
          "&pindex=" + GridView1.PageIndex.ToString() + " '}</script>", false);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bind(Session["sDL"].ToString(), Session["sTB"].ToString());
    }
    public void bind(string seField,string seValue)
    {
        string mysql = "select * from PatTb where "+seField+" like '%" + seValue + "%' ";
        OleDbCommand mycmd = new OleDbCommand(mysql,myConnection);
        OleDbDataAdapter Adapter = new OleDbDataAdapter();
        Adapter.SelectCommand = mycmd;
        DataSet myDS = new DataSet();
        Adapter.Fill(myDS);
        if (myDS.Tables.Count > 0 && myDS.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = myDS;
            GridView1.DataKeyNames = new string[] { "ID", "ID_JZ", "Name", "Age", "Snum", "Phone", "Addresss","Type","Sex" };
            GridView1.DataBind();
            Label2.Text = "";
        }
        else
        {
            Label2.Text = "无法查询到相关数据";
            GridView1.DataSource = null;
            GridView1.DataBind();

        }

    }
    public void delete(string myNo)
    {
        string mystr = "delete from PatTb where ID_JZ='" + myNo + "' ";
        OleDbCommand mycmd = new OleDbCommand(mystr, myConnection);
        mycmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;

        mystr = "update GH set ID_JZ='',Name='',Age='', Type='',Sex='' where ID_JZ='" + myNo + "' ";
        mycmd = new OleDbCommand(mystr, myConnection);
        mycmd.ExecuteNonQuery();

       // mystr = "update CourseTb set sNo='' ,sName='' where sNo='" + myNo + "' ";
      //  mycmd.ExecuteNonQuery();
       // mycmd.ExecuteNonQuery();

       // mystr = "update SelcourseTb set sNo='' ,sName='' where sNo='" + myNo + "'";
       // mycmd.ExecuteNonQuery();
       // mycmd.ExecuteNonQuery();

        bind(Session["sDL"].ToString(), Session["sTB"].ToString());

        TextBox1.Text = Session["sTB"].ToString();
        if (Session["sTB"].ToString() == "ID_JZ")
            DropDownList1.SelectedIndex = 0;
        else if (Session["sTB"].ToString() == "Name")
            DropDownList1.SelectedIndex = 1;
        else if (Session["sTB"].ToString() == "Age")
            DropDownList1.SelectedIndex = 2;
        else if (Session["sTB"].ToString() == "Type")
            DropDownList1.SelectedIndex = 3;
        else if (Session["sTB"].ToString() == "Sex")
            DropDownList1.SelectedIndex = 4;
        else
            DropDownList1.SelectedIndex = 5;
    }
}
